<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14
 * Time: 06:45 AM
 */
class PHZ_MoPai_Eat
{
    public static function __eat( $room_id , $who_uid , $card_number , $eat_card , $db_connect ){
        //取出房间信息
        $room_info    = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //当前用户是否有吃牌的权限
        $user_index   = PHZ_Tools::__get_user_in_room_index( $room_info , $who_uid );
        if( $room_info['p'. $user_index .'_is_chi'] != 1 ){
            return false;
        }
        //取出该房间当前这一局的手牌信息
        $card_info    = $db_connect->select('*')->from('tt_room_card')->where("jid = '". $room_info['room_js'] ."' and rid= '". $room_id ."' ")->orderByASC( array('id') )->query();
        //设置当前房间摸牌人以及牌号 - 被吃掉了，清空
        PHZ_Room::__setThisMopCard( $room_info['room_id'] , 0 , 0 , true , $db_connect );
        //取出吃牌者的手牌信息
        $eat_card_info   = array();
        foreach( $card_info as $key => $value ){
            if( $value['uid'] == $who_uid ){$eat_card_info = $value;}
        }
        //吃牌者增加下牌
        $eat_xp = $eat_card_info['xp'] . $eat_card . ",";
        $db_connect->update('tt_room_card')->cols(array('xp'=> $eat_xp ))->where('id = '. $eat_card_info['id'])->query();
        //删除吃牌者手中的手牌
        $new_sp = PHZ_Tools::__chi_pai_update_sp( $eat_card_info , $card_number , $eat_card , $db_connect );
        $db_connect->update('tt_room_card')->cols(array('sp'=> $new_sp ))->where('id = '. $eat_card_info['id'])->query();
        //既然该牌被吃；那么就可以取消所有人的吃牌跟碰牌状态了（为什么可以取消碰牌，因为碰优先；碰的人不碰，吃的人是吃不了的）
        //既然该牌被成功吃了；那么是可以取消胡，碰，跟所有吃的操作的
        PHZ_Tools::__set_room_alluser_hu_chi_peng_0( $room_id , $db_connect );//属性归0
        //增加息数(注意比牌问题)
        $temp_eat_card = explode(",", $eat_card );
        if( count( $temp_eat_card ) >= 2 ){
            foreach ( $temp_eat_card as $key => $value ){
                PHZ_SuanXi::__xi($room_info, $who_uid, "chi", $value, $db_connect);//迭代算息
            }
        }else {
            PHZ_SuanXi::__xi($room_info, $who_uid, "chi", $eat_card, $db_connect);//单吃
        }
        //用户是否有进字免打的权限？如果有；则直接下家摸牌操作
        $mianda = false;
        if( PHZ_Card::__get_User_TIPAO_Free_to_Play( $room_info , $who_uid , $db_connect ) == true ){
            $mianda = true;
        }
        //对所有人广播吃牌确认信息
        $temp = array();
        $temp['pl']          = 9;//9为用户确认吃牌后的广播
        $temp['card_number'] = (int)$card_number;//被吃的这个牌的名称
        $temp['put_uid']     = $mianda == true ? 0 : $who_uid;
        $temp['mop_uid']     = 0;
        $temp['eat_card']    = $eat_card;
        PHZ_Log::__send_user_log( $room_info , $who_uid , $temp , $db_connect );
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
        //如果免打，则吃碰畏牌的人下家自动摸牌(2017.11.21)
        if( $mianda == true ){
            PM_n::__index( $room_info , $who_uid , 0 , $db_connect );
        }
        //返回true
        return true;
    }
}